package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.OperationDict;
import com.zp.base.entity.bo.OperationDictBo;
import com.zp.base.entity.vo.OperationDictVo;
import com.zp.base.mapper.OperationDictMapper;
import com.zp.base.service.IOperationDictService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 手术操作字典Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-13
 */
@RequiredArgsConstructor
@Service
public class OperationDictServiceImpl implements IOperationDictService {

    private final OperationDictMapper baseMapper;

    /**
     * 查询手术操作字典
     */
    @Override
    public OperationDictVo queryById (String operationDictId) {
        return baseMapper.selectVoById(operationDictId);
    }

    /**
     * 查询手术操作字典列表(分页)
     */
    @Override
    public TableDataInfo<OperationDictVo> queryPageList (OperationDictBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OperationDict> lqw = buildQueryWrapper(bo);
        Page<OperationDictVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询手术操作字典列表(不分页)
     */
    @Override
    public List<OperationDictVo> queryList (OperationDictBo bo) {
        LambdaQueryWrapper<OperationDict> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OperationDict> buildQueryWrapper (OperationDictBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OperationDict> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOperationCode()), OperationDict::getOperationCode, bo.getOperationCode());
        lqw.like(StringUtils.isNotBlank(bo.getOperationName()), OperationDict::getOperationName, bo.getOperationName());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationScale()), OperationDict::getOperationScale, bo.getOperationScale());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), OperationDict::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicId()), OperationDict::getClinicId, bo.getClinicId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), OperationDict::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationFlag()), OperationDict::getOperationFlag, bo.getOperationFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), OperationDict::getOrgId, bo.getOrgId());
        return lqw;
    }

    /**
     * 新增手术操作字典
     */
    @Override
    public Boolean insertByBo (OperationDictBo bo) {
        OperationDict add = BeanUtil.toBean(bo, OperationDict.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOperationDictId(add.getOperationDictId());
        }
        return flag;
    }

    /**
     * 修改手术操作字典
     */
    @Override
    public Boolean updateByBo (OperationDictBo bo) {
        OperationDict update = BeanUtil.toBean(bo, OperationDict.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (OperationDict entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除手术操作字典
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 手术项目自动补全
     *
     * @param bo
     * @return
     */
    @Override
    public List<OperationDictVo> getOperationAuto (OperationDictBo bo) {
        return baseMapper.selectVoList(Wrappers.<OperationDict>lambdaQuery()
                .eq(OperationDict::getOrgId, bo.getOrgId())
                .and(StringUtils.isNotEmpty(bo.getItemInfo()), wrapper -> wrapper.like(OperationDict::getOperationName, bo.getItemInfo())
                        .or()
                        .like(OperationDict::getOperationCode, bo.getItemInfo())
                        .or()
                        .like(OperationDict::getInputCode, bo.getItemInfo()))
                .last("order by length(operation_name) asc limit 20")
        );
    }
}
